// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on('SRM Handboard Request', {
	refresh: function(frm) {
        if ( frappe.user_roles.includes('SRM Purchase') && frm.doc.workflow_state == 'Hand In') {
            frm.add_custom_button(__('费用确认'), () => {
                frappe.prompt('Amount', ({ value }) => {frm.set_value('amount_of_money', value); frm.refresh_field('amount_of_money'); frm.save();})
            });
        };
        
		frm.add_custom_button(__('返回公共查询'), () => {
			frappe.set_route('query-report', 'SRM Handboard Request Rep', {})
		}).addClass('btn-primary');

	},

	before_workflow_action: (frm) => {
		if ( frm.doc.workflow_state ==="Hand In" && frm.selected_workflow_action === "Approve" && frm.doc.amount_of_money == 0) {
			console.log(frm.selected_workflow_action);
            frappe.throw("金额为0不允许审核");
		}
	},

	
});


	// 	frm.add_custom_button(__('审批流日志'), () => {
	// 		frappe.call({
    //             method: "srm.srm.doctype.srm_handboard_request.srm_handboard_request.get_workflow_info",
    //             args: {
    //                 doctype: frm.doctype, 
	// 				reference_name: frm.doc.name,
    //             },
    //             callback: function(r) {
    //                 if (r.message.length > 0){
    //                     const d = new frappe.ui.Dialog({
    //                         title: __('审批流日志'),
    //                         width: 400
    //                     });
    //                     $(`<div class="modal-body ui-front">
    //                         <h3>${frm.doc.name}</h3>
    //                         <table class="table table-bordered">
    //                         <thead>
    //                             <tr>
    //                             <th>操作人</th>
    //                             <th>审批节点</th>
    //                             <th>日期</th>
    //                             </tr>
    //                         </thead>
    //                         <tbody>
    //                         </tbody>
    //                         </table>
    //                     </div>`).appendTo(d.body);
    //                     r.message.forEach(element => {
    //                         const tbody = $(d.body).find('tbody');
    //                         const tr = $(`
    //                         <tr>
    //                             <td>${element.full_name}</td>
    //                             <td>${element.creation}</td>
    //                             <td>${element.action}</td>
    //                         </tr>
    //                         `).appendTo(tbody)
    //                     });
    //                     cur_frm.rec_dialog = d;
    //                     d.show();  
    //                 }
    //             }
    //         });    
	// 	})